<?php

use Core\API_Controller;
use Models\Cashier;

class LoginController extends API_Controller
{
	public function index()
	{
		$this->load->library('form_validation');
		$this->form_validation->set_rules('username', '用户名', 'required')
		->set_rules('password', '密码', 'required');
		$lang = $this->input->post('lang');
		if(!$lang) $lang = 'chinese';
		if ($this->form_validation->run()) {
			$cashier = new Cashier;
			$cashier->where('username', $this->input->post('username'))->get_one();
			$pwd = encrypt($this->input->post('password'));
			if ($pwd == $cashier['password']) {
				$cashier->login();
				$this->session->cashier = $cashier;
				$this->session->lang = $lang;
				return $this->success();
			} else {
				return $this->error($pwd);
			}
		} else {
			return $this->validateError($this->form_validation->error_string());
		}
	}
}
